﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RimeLoRaTool
{
    public class WebClientEx : WebClient
    {
        /// <summary>
        /// 连接超时时间 单位 ms
        /// </summary>
        public int ConnectTimeout { get; set; }

        /// <summary>
        /// 读写超时时间 单位 ms
        /// </summary>
        public int ReadWriteTimeout { get; set; }

        /// <summary>
        /// 默认超时时间：连接 30 秒，读写 120 秒
        /// </summary>
        public WebClientEx() : this(30000, 120000)
        {
        }

        /// <summary>
        /// 单位 ms
        /// </summary>
        /// <param name="connectTimeout"></param>
        public WebClientEx(int connectTimeout) : this(connectTimeout, 12000)
        {
        }

        /// <summary>
        /// 单位 ms
        /// </summary>
        /// <param name="connectTimeout"></param>
        /// <param name="readWriteTimeout"></param>
        public WebClientEx(int connectTimeout, int readWriteTimeout)
        {
            ConnectTimeout = connectTimeout;
            ReadWriteTimeout = readWriteTimeout;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = (HttpWebRequest)base.GetWebRequest(address);
            request.Timeout = ConnectTimeout;
            request.ReadWriteTimeout = ReadWriteTimeout;
            return request;
        }
    }
}
